﻿using System.Collections;
using System.Linq;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Services.FinancialServices;
using Pap.Web.Utils;
using System.Collections.Generic;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class AssociationDepositesViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdministrationRepository;
        /// <summary>
        /// 
        /// </summary>
        IFinanceService _FinanceService;

        /// <summary>
        /// Initializes a new instance of the <see cref="AssociationDepositesViewModel"/> class.
        /// </summary>
        /// <param name="administrationRepository">The administration repository.</param>
        /// <param name="financeService">The finance service.</param>
        public AssociationDepositesViewModel(IAdministrationRepository administrationRepository, IFinanceService financeService)
        {
            _AdministrationRepository = administrationRepository;
            _FinanceService = financeService;
        }

        /// <summary>
        /// Gets the local committee deposites.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetLocalCommitteeDeposites()
        {
            var localCommittee = _AdministrationRepository.GetCommitteeByID(RegistryInfo.CommitteeID);
            IEnumerable<IProject> userProjects = _AdministrationRepository.GetUserProjects(RegistryInfo.UserID);
            var AllDeposites = localCommittee.CommitteeBankAccounts.SelectMany(banAccount => banAccount.CommitteeBankAccountDeposites).OrderBy(bA => bA.DepositDate);
            var projectDeposits = AllDeposites.Where(bDesposit => userProjects.Contains(bDesposit.CommitteeBankAccount.Project));
            return from deposit in projectDeposits
                   select new
                   {
                       CommitteeBankAccountDepositID = deposit.CommitteeBankAccountDepositID,
                       DepositeDate = deposit.DepositDate,
                       ProjectName = deposit.CommitteeBankAccount.Project.ProjectName,
                       DepositeAmount = deposit.CommitteeBankAccountDepositReceipts.Select(depoRec => depoRec.Receipt.ReceiptAmount).Sum()
                   };
        }

        /// <summary>
        /// Gets the committee balance.
        /// </summary>
        /// <returns></returns>
        public double GetCommitteeBalance()
        {
            return _FinanceService.GetCommitteeBalance(RegistryInfo.CommitteeID);
        }

        /// <summary>
        /// Gets the name of the committee.
        /// </summary>
        /// <returns></returns>
        public string GetCommitteeName()
        {
            var committee = _AdministrationRepository.GetCommitteeByID(RegistryInfo.CommitteeID);
            return committee.CommitteeName;
        }
    }
}
